<script setup>
import { useTargetIndex, useComputedData, useRightListData, useCheckedData, useDragedItem } from './extends/hooks';
import Selector from './components/Selector';
import ListTitle from './components/ListTitle';
import ButtonGroup from './components/ButtonGroup';
const props = defineProps({
  data: {
    type: Array,
    default: () => [],
  },
  rightTitle: {
    type: String,
    default: String,
  },
});

const options = props.data.map((v) => v.title);

const [targetIndex, setTargetIndex] = useTargetIndex(0);

const { checkedData, addCheckData, removeCheckData } = useCheckedData();

const [rightListData, addRightListData, removeRightListData] = useRightListData([], checkedData);

const { dragedItem, setDragedItem } = useDragedItem();

const { leftTitle, leftListData, transferButtonDisabled, checkedAllBool } = useComputedData(props.data, targetIndex, rightListData, checkedData);

// 点击左侧列表的checkbox触发
// checkBol:是否选中
// leftOrRight:左侧还是右侧
// item:当前点击的item
const setCheckData = (checkBol, leftOrRight, item) => {
  checkBol ? addCheckData(leftOrRight, item) : removeCheckData(leftOrRight, item);
};

// 点击全选框,选中全部
const setCheckDataAll = (checkBol, leftOrRight) => {
  // computed返回到是ref
  checkBol
    ? addCheckData(leftOrRight, leftOrRight === 'left' ? leftListData.value : rightListData.value, true)
    : removeCheckData(leftOrRight, leftOrRight === 'left' ? leftListData.value : rightListData.value, true);
};
</script>

<template>
  <div>
    <Selector :data="options" @setTargetChange="setTargetIndex"></Selector>
  </div>
  <div class="transfer">
    <div class="box left-list" @dragover.prevent @drop="removeRightListData([dragedItem])">
      <div class="head">
        <input type="checkbox" :checked="checkedAllBool.left" @click="setCheckDataAll($event.target.checked, 'left')" />
        <List-title :title="leftTitle"></List-title>
      </div>
      <Button-group
        :list-data="leftListData"
        :checked-data="checkedData.left"
        left-or-right="left"
        @setCheckData="setCheckData"
        @setDragedItem="setDragedItem"></Button-group>
    </div>
    <div class="box button-group">
      <button :disabled="transferButtonDisabled.left" @click="removeRightListData(checkedData.right)">&lt;</button>
      <button :disabled="transferButtonDisabled.right" @click="addRightListData(checkedData.left)">&gt;</button>
    </div>
    <div class="box right-list" @dragover.prevent @drop="addRightListData([dragedItem])">
      <div class="head">
        <input type="checkbox" :checked="checkedAllBool.right" @click="setCheckDataAll($event.target.checked, 'right')" />
        <List-title :title="rightTitle"></List-title>
      </div>
      <Button-group
        :list-data="rightListData"
        :checked-data="checkedData.right"
        left-or-right="right"
        @setCheckData="setCheckData"
        @setDragedItem="setDragedItem"></Button-group>
    </div>
  </div>
</template>

<style lang="less" scoped>
.transfer {
  width: 360px;
  height: 300px;
  display: flex;
  border: 1px solid #000;

  .box {
    width: 120px;
    height: 100%;

    .head {
      width: 100%;
      display: flex;
      justify-content: start;
      align-items: center;
      height: 38px;
      margin: 0;
      font-weight: normal;
      color: #666;
      border: 1px solid #ddd;
      background-color: #efefef;
      font-size: 14px;
    }

    &.button-group {
      display: flex;
      justify-content: center;
      align-items: center;
      border-left: 1px solid #ddd;
      border-right: 1px solid #ddd;
      button {
        border: none;
        width: 30px;
        height: 30px;
        background-color: orange;
        color: #fff;
        border-radius: 5px;
        margin: 5px;

        &:disabled {
          opacity: 0.6;
        }
      }
    }
  }
}
</style>
